package com.example.mallstable.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mallstable.R;
import com.example.mallstable.adapter.ConfirmOrderProductAdapter;
import com.example.mallstable.adapter.OrderIndetailAdapter;
import com.example.mallstable.config.Constant;
import com.example.mallstable.pojo.ActionOrderVo;
import com.example.mallstable.pojo.ActionorderItemVo;
import com.example.mallstable.pojo.Address;
import com.example.mallstable.pojo.Cart;
import com.example.mallstable.pojo.CartItem;
import com.example.mallstable.pojo.Order;
import com.example.mallstable.pojo.ResponeCode;
import com.example.mallstable.pojo.SverResponse;
import com.example.mallstable.utils.JSONUtils;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;


public class OrderIndetailActivity extends AppCompatActivity {
    private TextView name;
    private TextView mobile;
    private TextView addr_detail;
    private TextView total;
    private Toolbar toolbar;

    private RecyclerView recyclerView;
    private OrderIndetailAdapter orderIndetailAdapter;
    private Address defaultAddr;
    private String order="";
    private int orderno=0;
    private Object orders;
    private String names;
    private String address;
    private String leixing;
    private String zhuangtai;
    private String amount;
    private List<ActionorderItemVo> cartItems =new ArrayList<>();
    private int status;
    private String ddzhuangtai="";




    private void niu2(){
    orderIndetailAdapter = new OrderIndetailAdapter(this, cartItems);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(orderIndetailAdapter);

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_indetail);

        System.out.println("跳转后");
        Intent intent=getIntent();
        order=intent.getStringExtra("orderno");
        names=intent.getStringExtra("name");
     //   address=intent.getStringExtra("address");
        leixing=intent.getStringExtra("leixing");
        zhuangtai=intent.getStringExtra("zhuangtai");
        amount=intent.getStringExtra("amount");

        System.out.println(order);
        System.out.println(zhuangtai);
     //   System.out.println(address);


        System.out.println("图");
        submitOrder(order);
        System.out.println("view");
        initView();
        System.out.println("加载地址");


    }

    private void initView() {
        name = findViewById(R.id.name);
        mobile = findViewById(R.id.mobile);
        addr_detail = findViewById(R.id.addr_detail);
        recyclerView = findViewById(R.id.cart_rv);
        total = findViewById(R.id.total);
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("支付订单信息");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        //支付订单
        findViewById(R.id.buy_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ddzhuangtai="已付款";
                mobile.setText(leixing+"                  "+ddzhuangtai);
                Toast.makeText(OrderIndetailActivity.this,"订单支付成功",Toast.LENGTH_LONG).show();
            }
        });

    }


    /**
     * 显示地址信息
     * @param status
     */
    private void displayInfo(int status) {

        if(status==1)
        {
            ddzhuangtai="未付款";
        }
        name.setText("订单号"+order);
        mobile.setText(leixing+"                  "+ddzhuangtai);
        addr_detail.setText("购买人"+names+"金额为"+amount);


    }




    private void submitOrder(String no) {
        OkHttpUtils.post()
                .url(Constant.API.ORDER_DETAIL_URL)
                .addParams("orderNo",no)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(OrderIndetailActivity.this,"订单确定",Toast.LENGTH_LONG).show();
                        Log.e("订单确定",e.toString());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("订单确定","订单确定成功");
                        Type type = new TypeToken<SverResponse<ActionOrderVo>>() {
                        }.getType();
                        SverResponse<ActionOrderVo> result = JSONUtils.formJson(response, type);
                        System.out.println(result.getData().getOrderItems());


                        if (result.getStatus() == ResponeCode.SUCCESS.getCode()) {
                            Toast.makeText(OrderIndetailActivity.this, "订单展示成功", Toast.LENGTH_LONG).show();

                            cartItems.clear();
                            //cartItems.add();
                            cartItems.addAll(result.getData().getOrderItems());
                            status= result.getData().getStatus();
                            displayInfo(status);
                            niu2();
                            orderIndetailAdapter.notifyDataSetChanged();
                           // finish();
                            total.setText("合计：" + result.getData().getAmount());
                        } else {
                            Toast.makeText(OrderIndetailActivity.this, result.getStatus(), Toast.LENGTH_LONG).show();
                        }


                    }
                });

    }
}
